package com.xu.crawler.listener;

import com.xu.crawler.dao.FailRecordDao;
import com.xu.crawler.dao.entity.FailRecordDO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.SpiderListener;

import javax.annotation.Resource;

/**
 * Created by clear on 2018/7/17.
 */
@Component
public class DownloadFailListener implements SpiderListener {

    private Logger log = LoggerFactory.getLogger(DownloadFailListener.class);
    @Resource
    private FailRecordDao failRecordDao;

    @Override
    public void onSuccess(Request request) {

    }

    @Override
    public void onError(Request request, String errorMsg) {
        try {
            FailRecordDO failRecordDO = new FailRecordDO();
            failRecordDO.setUrl(request.getUrl());
            failRecordDO.setReason(errorMsg);
            failRecordDao.saveFailRecord(failRecordDO);
        } catch (Exception e) {
            log.error("DownloadFailListener onError fail: {}", e);
        }
    }
}
